package com.danceforyou.common.util;

import org.mvel2.MVEL;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 计算工具
 * @author danceforyou
 */
public class CalculateUtil {

    /**
     * 根据传入的公式和参数进行计算
     * @param formula 公式
     * @param variables 参数
     * @return BigDecimal
     */
    public static BigDecimal calculate(String formula, Map<String, BigDecimal> variables) {
        try {
            Object object = MVEL.eval(formula, variables);
            BigDecimal result;
            if (object instanceof Integer) {
                result = BigDecimal.valueOf((Integer) object);
            } else if (object instanceof Double) {
                result = BigDecimal.valueOf((Double) object);
            } else if (object instanceof BigDecimal) {
                result = (BigDecimal) object;
            } else if (object instanceof Boolean) {
                if ((Boolean) object) {
                    result = BigDecimal.ZERO;
                } else {
                    return null;
                }
            } else {
                result = new BigDecimal(object.toString());
            }
            return result.setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String formula = "if (g == 0) {\n" +
                "            return a - b;\n" +
                "        } else  if (h == 0) {\n" +
                "            if (g == -1 || g == 1) {\n" +
                "                return a - b  - a/1.01*0.02;\n" +
                "            } else if (g == -3 || g == 3) {\n" +
                "                return a - b - a/1.03*0.04;\n" +
                "            } else if (g == -6 || g == 6) {\n" +
                "                return a - b - a/1.06*0.05;\n" +
                "            }\n" +
                "        } else if (g == -1 || g == 1) {\n" +
                "            return a - b - a/1.01*0.02;\n" +
                "        } else if (g == -3 || g == 3) {\n" +
                "            return a - b - a/1.03*0.04;\n" +
                "        } else if (g == -6 || g == 6) {\n" +
                "            if (h > 0) {\n" +
                "                return a- b - c + d - (a/1.06 - b/(1+h/100))*0.03;\n" +
                "            } else if (h < 0) {\n" +
                "                return a - b - c - (a/1.06 - b/(1+(0-h)/100)*0.03);\n" +
                "            }\n" +
                "        }\n" +
                "        return 0;";
        Map<String, BigDecimal> variables = new HashMap<>();
        variables.put("a", new BigDecimal("5580"));//执行价
        variables.put("b", new BigDecimal("5000"));//成本价
        variables.put("c", new BigDecimal("315.85"));//客户税额
        variables.put("d", new BigDecimal("283.02"));//号主税额
        variables.put("e", BigDecimal.ZERO);//客户返点
        variables.put("f", BigDecimal.ZERO);//号主返点
        variables.put("g", new BigDecimal(6));//客户税点
        variables.put("h", new BigDecimal(6));//号主税点
        System.out.println(calculate(formula, variables));
    }
}